/*
 * Copyright (c) 2007 T.Cogghe, D.Leroux, S.Lévignac, S.Rives
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include "fenetreFichiers.hh"
#include "fenetreAstres.hh"
#include "fenetreDark.hh"
#include "fenetreTraitement.hh"
#include "dialogueErreurForme.hh"
#include "dialogueErreurMatrice.hh"
#include "IHM/editAstres.hh"
#include "Forme.hh"


IHM::FenetreAstres::FenetreAstres(BaseObjectType* cobject, const Glib::RefPtr<Gnome::Glade::Xml>& refGlade)
	: Gtk::Window(cobject), //m_refGlade(refGlade),
		imgAstres(0), validAst(0), evBox(0), sw(0)
{
	Gtk::Button*b;
	getW(refGlade,b,"b_Ast_Precedent");
	b->signal_clicked().connect(sigc::mem_fun(*this,&FenetreAstres::precedent));
	getW(refGlade,b,"b_Ast_Valider");
	b->signal_clicked().connect(sigc::mem_fun(*this,&FenetreAstres::valider));
	getW(refGlade,evBox,"evBoxEditAstres");
	evBox->signal_button_press_event().connect(sigc::mem_fun(*this,&FenetreAstres::bouton));
	evBox->signal_motion_notify_event().connect(sigc::mem_fun(*this,&FenetreAstres::mouvement));
	getW(refGlade,imgAstres,"editAstres");
	getW(refGlade,validAst,"b_Ast_Valider");

	getW(refGlade,sw,"fAst_fimg");
}

IHM::FenetreAstres::~FenetreAstres() {}

int IHM::FenetreAstres::mouvement(GdkEventMotion*ev) {
	return 0;
}

int IHM::FenetreAstres::bouton(GdkEventButton*ev) {
	static Astre*a=NULL,*b=NULL;
	//cout<<"bouton enfoncé #"<<ev->button<<endl;
	/* conversion des coordonnées fenêtre en coordonnées image si besoin est */
	int x,y;
	if(sw->get_width()>(**IHM::env->edAs).largeur()) {
		ev->x -= ( sw->get_width() - (**IHM::env->edAs).largeur() ) / 2;
	}
	if(sw->get_height()>(**IHM::env->edAs).hauteur()) {
		ev->y -= ( sw->get_height() - (**IHM::env->edAs).hauteur() ) / 2;
	}
	if(ev->button==1) {	/* bouton gauche */
		IHM::env->edAs->ajouterAstre((int)ev->x,(int)ev->y);
	} else if(ev->button==2) {
		/* FIXME : debug only */
		if(!a) {
			//Image::Seuil k(&**edAs);
			a=Astre::trouverAutourDe(**IHM::env->edAs,(int)ev->x,(int)ev->y,IHM::env->edAs->k());
		} else {
			//Image::Seuil k(&**edAs);
			if(b) delete b;
			b=Astre::trouverAutourDe(**IHM::env->edAs,(int)ev->x,(int)ev->y,IHM::env->edAs->k());
			if(b) {
				cout<<"test %   ===>   "<< (a->id()%b->id()) <<endl;
			}
		}
	} else if(ev->button==3) {	/* bouton droit */
		IHM::env->edAs->enleverAstre((int)ev->x,(int)ev->y);
	}

	return 0;
}

void IHM::FenetreAstres::precedent() {
	IHM::env->fAst->hide();
	IHM::env->fFic->show();
}

void IHM::FenetreAstres::valider() {
//	for(int i=0;i<3;i++) {
//		images[imgDetection]->pointsCle()[i].x=edAs->astre(i)->x();
//		images[imgDetection]->pointsCle()[i].y=edAs->astre(i)->y();
//	}
	if(IHM::env->images[IHM::env->imgDetection]->forme()) {
		delete IHM::env->images[IHM::env->imgDetection]->forme();
	}

	try {
		IHM::env->images[ IHM::env->imgDetection ]->forme() =
			new Forme( IHM::env->edAs->astre(0), IHM::env->edAs->astre(1), IHM::env->edAs->astre(2) );
	
		if(!(IHM::env->images[0]->forme()->id()==IHM::env->images[IHM::env->imgDetection]->forme()->id())) {
			cout<<"Forme invalide ! Il faut recommencer."<<endl;
			IHM::env->dErrForme->run();
			return;
		}
	} catch(exception*e) {
		cout<<"Forme invalide ! Il faut recommencer."<<endl;
		return;
	}

	/*testing*/
	if(IHM::env->imgDetection) {
		double delta= 100.*(IHM::env->images[0]->forme()->id() % IHM::env->images[IHM::env->imgDetection]->forme()->id());
		cout<<'#'<<IHM::env->imgDetection<<" : Discordance entre les formes de "<<delta<<'%'<<endl; 
	}

	if(IHM::env->autoDetection||IHM::env->imgDetection==(int)IHM::env->images.size()-1) {
		/* réinitialise la détection des formes si besoin est, le traitement n'aura
		 * pas à tout recalculer si on n'a changé que les réglages du Dark
		 */
		IHM::env->formesDetectees=!IHM::env->autoDetection;
		hide();
		IHM::env->fDark->show();
	} else {
		char buf[64];
		++IHM::env->imgDetection;
		sprintf(buf,"Superpaws - Sélection des points-clé - image %i/%i",
			IHM::env->imgDetection+1,(int)IHM::env->images.size());
		IHM::env->fAst->set_title(Glib::ustring(buf));
		
		IHM::env->edAs->setImage(*IHM::env->images[IHM::env->imgDetection]);
//		if(edAs) delete edAs;
//		edAs=new EditAstres(g->imgAstres,g->validAst,*images[imgDetection]);
		IHM::env->edAs->griserValid();
		
	}
}

